import 'package:flutter/material.dart';
import 'package:flutter_app/config/app_colors.dart';
import 'package:flutter_app/util/show.dart';
import 'package:flutter_app/view/root_page/home_page.dart';
import 'package:flutter_app/view/root_page/music_page.dart';
import 'package:flutter_app/view/root_page/profile_page.dart';
import 'package:flutter_app/view/root_page/tiny_radio_page.dart';
import 'package:proste_indexed_stack/proste_indexed_stack.dart';

class RootPage extends StatefulWidget {
  RootPage({Key key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => RootPageState();
}

const Map<String, String> _bottomNames = {
  'home': '首页',
  'music': '音乐',
  'create_media': '',
  'tiny_video': '小视频',
  'profile': '我的',
};

class RootPageState extends State<RootPage> {
  int _currentIndex = 0;

  //页面集合
  final List<IndexedStackChild> _pages = [
    IndexedStackChild(child: HomePage()),
    IndexedStackChild(child: MusicPage()),
    IndexedStackChild(child: Container()),
    IndexedStackChild(child: TinyRadioPage()),
    IndexedStackChild(child: ProfilePage()),
  ];
  final List<BottomNavigationBarItem> _bottomNavBarList = [];

  @override
  void initState() {
    super.initState();
    _bottomNames.forEach((key, value) {
      _bottomNavBarList.add(_bottomNavBatItem(key, value));
    });
  }

  ///底部切换
  void _onTabClick(int index) {
    if (index == 2) {
      return _onCreateMedia();
    }
    setState(() {
      _currentIndex = index;
    });
  }

  ///点击发布按钮
  void _onCreateMedia() {
    showToast('object');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ProsteIndexedStack(
        index: _currentIndex,
        children: _pages,
      ), //IndexedStack缓存组件
      bottomNavigationBar: BottomNavigationBar(
        items: _bottomNavBarList,
        currentIndex: _currentIndex,
        onTap: _onTabClick,
        type: BottomNavigationBarType.fixed, //底部按钮布局整齐
      ),
      floatingActionButton: _createMediaButton(), //按钮变大
      floatingActionButtonLocation:
          FloatingActionButtonLocation.centerDocked, //按钮的位置
    );
  }

  ///发布按钮
  Widget _createMediaButton() {
    return Container(
      width: 44,
      height: 44,
      margin: EdgeInsets.only(top: 56),
      child: FloatingActionButton(
        backgroundColor: AppColors.nav,
        child: Image.asset(
          'assets/images/icons/create_media.png',
        ),
        onPressed: _onCreateMedia,
      ),
    );
  }

  ///底部导航每一项的组件
  BottomNavigationBarItem _bottomNavBatItem(String key, String value) {
    return BottomNavigationBarItem(
        icon:
            Image.asset('assets/images/icons/$key.png', width: 24, height: 24),
        activeIcon: Image.asset('assets/images/icons/${key}_active.png',
            width: 24, height: 24),
        label: value,
        tooltip: ''); //tooltip长按不提示
  }
}
